package show.me.the.money.config.app;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Element;

import show.me.the.money.config.ElementConfig;



public class Application extends ElementConfig{
	private Element define;
	private HashMap<String,Module> modules = new LinkedHashMap<String,Module>();

	@SuppressWarnings("unchecked")
	public void setDefineElement(Element define){
		this.define = define;
		if(define != null){
			id = define.attributeValue("id");
			List<Element> els = define.elements("module");
			int n = els.size();
			for(int i = 0 ; i < n; i ++){
				Element el = els.get(i);
				String id = el.attributeValue("id");
				Module module = new Module(el);
				module.setIndex(i);
				module.setApp(this);
				modules.put(id, module);
			}
		}
	}
	
	public Element getDefineElement() {
		return define;
	}
	
	public Module module(String id){
		if(modules.containsKey(id)){
			return modules.get(id);
		}
		return null;
	}
		
	public List<Module> getAllModules(){
		List<Module> ls = new ArrayList<Module>();
		ls.addAll(modules.values());
		return ls;
	}
	
	@SuppressWarnings("unchecked")
	public HashMap<String,Object> data(){
		HashMap<String,Object> h = new HashMap<String,Object>();
		List<Attribute> attrs = define.attributes();
		for(Attribute attr:attrs){
			h.put(attr.getName(), attr.getText());
		}		
		return h;
	}

}
